package com.qf.dao;

import com.qf.pojo.Cart;
import com.qf.util.DataSourceUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author 千锋健哥
 */
public class CartDaoImpl implements CartDao{

    private QueryRunner queryRunner = new QueryRunner(DataSourceUtil.getDataSource());

    @Override
    public void insert(Cart cart) {
        try {
            String sql = "insert into tb_cart (uid, pid, num, money) values(?, ?, ?, ?)";
            queryRunner.update(sql, cart.getUid(), cart.getPid(), cart.getNum(), cart.getMoney());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<Cart> findByUid(Integer uid) {
        try {
            String sql = "select * from tb_cart where uid=?";
            List<Cart> list = queryRunner.query(sql, new BeanListHandler<Cart>(Cart.class), uid);
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void deleteByUid(Integer uid) {
        try {
            String sql = "delete from tb_cart where uid=?";
            queryRunner.update(sql, uid);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

}
